package com.winit.openapi.ece.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.ece.vo.EceMerchandiseVo;
import com.winit.oms.spi.ece.vo.EceOrderExpressInfoVo;
import com.winit.oms.spi.ece.vo.EceOrderInsuranceInfoVo;
import com.winit.oms.spi.ece.vo.EceOrderPackageVo;
import com.winit.oms.spi.ece.vo.EceOrderVo;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.ece.vo.EceOrderDispatchType;

/**
 * 进口订单构造器
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * fei.liu 	1.0  		2015年8月3日 	Created
 *
 * </pre>
 * @since 1.
 */
public class EceOrderBuilder {

    public static EceOrderVo buildEceOrder(JSONObject json) {
        EceOrderVo eceOrder = new EceOrderVo();

        /**************************** eceOrder ***********************/
        if (json.containsKey("paymentTransactionID")) {
            eceOrder.setPaymentTransactionId(json.getString("paymentTransactionID").trim());
        }
        // 批量更新才会有这个字段
        if (json.containsKey("orderNo")) {
            eceOrder.setOrderNo(json.getString("orderNo").trim());
        }

        if (json.containsKey("buyerCountryCode")) {
            eceOrder.setBuyerCountry(json.getString("buyerCountryCode").trim());
        }

        // 批量导入和批量更新公用下面的字段
        if (json.containsKey("warehouseName")) {
            eceOrder.setInspectionWarehouseName(json.getString("warehouseName").trim());
        }
        if (json.containsKey("winitProductCode")) {
            eceOrder.setWinitProductCode(json.getString("winitProductCode").trim());
        }
        // 卖家订单号
        if (json.containsKey("refNo")) {
            eceOrder.setSellerOrderNo(json.getString("refNo").trim());
        }
        if (json.containsKey("currency")) {
            eceOrder.setCurrency(json.getString("currency").trim());
        }
        if (json.containsKey("totalAmount")) {
            eceOrder.setTotalAmount(json.getBigDecimal("totalAmount"));
        }
        if (json.containsKey("dispatchType")) {
            eceOrder.setDispatchType(json.getString("dispatchType"));
        }
        /**************************** buyer ***************************/
        if (json.containsKey("buyerName")) {
            eceOrder.setBuyerFullname(json.getString("buyerName").trim());
        }
        if (json.containsKey("buyerContactNo")) {
            eceOrder.setBuyerContactNumber(json.getString("buyerContactNo").trim());
        }
        if (json.containsKey("buyerEmail")) {
            eceOrder.setBuyerEmail(json.getString("buyerEmail").trim());
        }
        if (json.containsKey("buyerZipCode")) {
            eceOrder.setBuyerPostcode(json.getString("buyerZipCode").trim());
        }
        // 是否保险 add by changjun.xiang 2016-4-20
        if (json.containsKey("isInsure")) {
            eceOrder.setIsInsure(json.getString("isInsure").trim());
        }
        // 版本号 add by changjun.xiang 2016-4-22
        if (json.containsKey("versionFlag")) {
            eceOrder.setVersionFlag(json.getString("versionFlag").trim());
        }
//        if (json.containsKey("insurance")) {
//            eceOrder.setInsurance(json.getBigDecimal("insurance"));
//        }
        if (json.containsKey("taxes")) {
            eceOrder.setTaxes(json.getBigDecimal("taxes"));
        }
        if (json.containsKey("freight")) {
            eceOrder.setFreight(json.getBigDecimal("freight"));
        }
        if (json.containsKey("goodsAmount")) {
            eceOrder.setGoodsAmount(json.getBigDecimal("goodsAmount"));
        }
        
        if (json.containsKey("buyerCountry")) {
            eceOrder.setBuyerCountry(json.getString("buyerCountry").trim());
        }
        if (json.containsKey("buyerState")) {
            eceOrder.setBuyerState(json.getString("buyerState").trim());
        }
        if (json.containsKey("buyerCity")) {
            eceOrder.setBuyerCity(json.getString("buyerCity").trim());
        }
        if (json.containsKey("buyerDistrict")) {
            eceOrder.setBuyerDistrict(json.getString("buyerDistrict").trim());
        }
        if (json.containsKey("buyerAddress1")) {
            eceOrder.setBuyerAddress1(json.getString("buyerAddress1").trim());
        }
        if (json.containsKey("buyerAddress2")) {
            eceOrder.setBuyerAddress2(json.getString("buyerAddress2").trim());
        }
        if (json.containsKey("buyerHouseNo")) {
            eceOrder.setHouseNo(json.getString("buyerHouseNo").trim());
        }
        //兼容老的PAI，
        if (json.containsKey("buyerCitizenIdNumeber")) {
            eceOrder.setBuyerCitizenIdNo(json.getString("buyerCitizenIdNumeber").trim());
        } else if (json.containsKey("buyerCitizenIdNumber")) {
            eceOrder.setBuyerCitizenIdNo(json.getString("buyerCitizenIdNumber").trim());
        }

        /**************************** shipper ***************************/
        if (json.containsKey("shipperAddrCode")) {
            eceOrder.setShipperAddrCode(json.getString("shipperAddrCode"));
        }

        if (json.containsKey("expressNoList")) {
            List<EceOrderExpressInfoVo> expressNoList = new ArrayList<EceOrderExpressInfoVo>();
            JSONArray eNList = json.getJSONArray("expressNoList");
            for (Object object : eNList) {
                JSONObject expressNoJson = (JSONObject) object;
                EceOrderExpressInfoVo expressNo = new EceOrderExpressInfoVo();
                if (expressNoJson.containsKey("expressNo")) {
                    expressNo.setExpressNo(expressNoJson.getString("expressNo").trim());
                }
                expressNoList.add(expressNo);
            }
            eceOrder.setExpressNoList(expressNoList);

        }

        /**************************** 包裹列表(商品) ***************************/
        List<EceOrderPackageVo> orderPkgList = new ArrayList<EceOrderPackageVo>();
        if (json.containsKey("orderPackageList")) {
            JSONArray pkgList = json.getJSONArray("orderPackageList");
            for (Object object : pkgList) {
                JSONObject orderPkg = (JSONObject) object;
                EceOrderPackageVo orderPkgVo = new EceOrderPackageVo();

                orderPkgVo.setLength(BigDecimal.ZERO);
                orderPkgVo.setWidth(BigDecimal.ZERO);
                orderPkgVo.setHeight(BigDecimal.ZERO);

                if (orderPkg.containsKey("grossWeight")) {
                    orderPkgVo.setGrossWeight(orderPkg.getBigDecimal("grossWeight"));
                }

                List<EceMerchandiseVo> merchandiseList = new ArrayList<EceMerchandiseVo>();
                if (orderPkg.containsKey("merchandiseList")) {
                    JSONArray mlist = orderPkg.getJSONArray("merchandiseList");
                    for (Object obj : mlist) {
                        JSONObject merchandise = (JSONObject) obj;
                        EceMerchandiseVo vo = new EceMerchandiseVo();
                        if (merchandise.containsKey("productCode")) {
                            vo.setMerchandiseCode(merchandise.getString("productCode").trim());
                        }
                        if (merchandise.containsKey("productName")) {
                            vo.setMerchandiseNameCn(merchandise.getString("productName").trim());
                        }
                        if (merchandise.containsKey("specification")) {
                            vo.setMerchandiseSpecification(merchandise.getString("specification").trim());
                        }
                        if (merchandise.containsKey("quantity")) {
                            vo.setMerchandiseQuantity(merchandise.getLong("quantity"));
                        }
                        if (merchandise.containsKey("salePrice")) {
                            vo.setSalePrice(merchandise.getBigDecimal("salePrice"));
                        }
                        if (merchandise.containsKey("currency")) {
                            vo.setCurrencyCode(merchandise.getString("currency").trim());
                        }
                        if (merchandise.containsKey("extraGift")) {
                            vo.setExtraGift(merchandise.getString("extraGift").trim());
                        }
                        if (merchandise.containsKey("brandName")) {
                            vo.setBrandName(merchandise.getString("brandName").trim());
                        }
                        merchandiseList.add(vo);
                    }
                    orderPkgVo.setMerchandiseList(merchandiseList);
                }
                orderPkgList.add(orderPkgVo);
            }
        }

        CommandContext ctx = CommandContext.getContext();
        String source = (String) ctx.get(ApiConstant.API_SOURCE);
        // 设置其他信息，从上下文取得
        eceOrder.setSource(source);
        eceOrder.setBpartnerId(Integer.parseInt(ctx.getUserId()));
        eceOrder.setBpartnerName(ctx.getUsername());
        eceOrder.setOrganizationId(ctx.getOrgId().intValue());

        eceOrder.setOrderPackageList(orderPkgList);
        return eceOrder;
    }

    /**
     * ( 兼容进口一期 )批量导入进口订单封装实体
     * 
     * @param json
     * @return
     */
    public static EceOrderVo buildISPImportOrder(JSONObject json) {
        EceOrderVo eceOrder = new EceOrderVo();

        /**************************** eceOrder ***********************/
        if (json.containsKey("paymentTransactionID")) {
            eceOrder.setPaymentTransactionId(json.getString("paymentTransactionID").trim());
        }
        if (json.containsKey("buyerCountryCode")) {
            eceOrder.setBuyerCountry(json.getString("buyerCountryCode").trim());
        }

        // 批量更新才会有这个字段
        if (json.containsKey("orderNo")) {
            eceOrder.setOrderNo(json.getString("orderNo").trim());
        }

        if (json.containsKey("warehouseID")) {
            eceOrder.setWarehouseId(json.getInteger("warehouseID"));
        }
        // 给定默认值发货方式：S
        eceOrder.setDispatchType(EceOrderDispatchType.SellerSend.getValue());

        if (json.containsKey("winitProductCode")) {
            eceOrder.setWinitProductCode(json.getString("winitProductCode").trim());
        }
        if (json.containsKey("refNo")) {
            eceOrder.setSellerOrderNo(json.getString("refNo").trim());
        }
        if (json.containsKey("currency")) {
            eceOrder.setCurrency(json.getString("currency").trim());
        }
        if (json.containsKey("totalAmount")) {
            eceOrder.setTotalAmount(json.getBigDecimal("totalAmount"));
        }
        /**************************** buyer ***************************/
        if (json.containsKey("buyerName")) {
            eceOrder.setBuyerFullname(json.getString("buyerName").trim());
        }
        if (json.containsKey("buyerContactNo")) {
            eceOrder.setBuyerContactNumber(json.getString("buyerContactNo").trim());
        }
        if (json.containsKey("buyerEmail")) {
            eceOrder.setBuyerEmail(json.getString("buyerEmail").trim());
        }
        if (json.containsKey("buyerAddress1")) {
            eceOrder.setBuyerAddress1(json.getString("buyerAddress1").trim());
        }
        if (json.containsKey("buyerAddress2")) {
            eceOrder.setBuyerAddress2(json.getString("buyerAddress2").trim());
        }
        if (json.containsKey("buyerCity")) {
            eceOrder.setBuyerCity(json.getString("buyerCity").trim());
        }
        if (json.containsKey("buyerState")) {
            eceOrder.setBuyerState(json.getString("buyerState").trim());
        }
        if (json.containsKey("buyerZipCode")) {
            eceOrder.setBuyerPostcode(json.getString("buyerZipCode").trim());
        }
        if (json.containsKey("buyerCountryCode")) {
            eceOrder.setBuyerCountry(json.getString("buyerCountryCode").trim());
        }
        if (json.containsKey("buyerHouseNo")) {
            eceOrder.setHouseNo(json.getString("buyerHouseNo").trim());
        }
        if (json.containsKey("buyerCitizenIdNumeber")) {
            eceOrder.setBuyerCitizenIdNo(json.getString("buyerCitizenIdNumeber").trim());
        }

        /**************************** shipper ***************************/
        if (json.containsKey("shipperAddrId")) {
            eceOrder.setShipperAddrId(json.getInteger("shipperAddrId"));
        }
        if (json.containsKey("shipperAddr")) {
            eceOrder.setShipperAddrCode(json.getString("shipperAddr"));
        }
        if (json.containsKey("shipperName")) {
            eceOrder.setShipperFullname(json.getString("shipperName"));
        }
        if (json.containsKey("shipperContactNo")) {
            eceOrder.setShipperContactNumber(json.getString("shipperContactNo"));
        }
        if (json.containsKey("shipperEmail")) {
            eceOrder.setShipperEmail(json.getString("shipperEmail"));
        }
        if (json.containsKey("shipperZipCode")) {
            eceOrder.setShipperPostcode(json.getString("shipperZipCode"));
        }

        if (json.containsKey("shipperCountry")) {
            eceOrder.setShipperCountry(json.getString("shipperCountry"));
        }
        if (json.containsKey("shipperCountryCode")) {
            eceOrder.setShipperCountryCode(json.getString("shipperCountryCode"));
        }
        if (json.containsKey("shipperState")) {
            eceOrder.setShipperState(json.getString("shipperState"));
        }
        if (json.containsKey("shipperStateCode")) {
            eceOrder.setShipperStateCode(json.getString("shipperStateCode"));
        }
        if (json.containsKey("shipperCity")) {
            eceOrder.setShipperCity(json.getString("shipperCity"));
        }

        if (json.containsKey("expressNoList")) {
            List<EceOrderExpressInfoVo> expressList = new ArrayList<EceOrderExpressInfoVo>();
            JSONArray eNList = json.getJSONArray("expressNoList");
            for (Object object : eNList) {
                JSONObject expressNoJson = (JSONObject) object;
                EceOrderExpressInfoVo express = new EceOrderExpressInfoVo();
                if (expressNoJson.containsKey("expressNo")) {
                    express.setExpressNo(expressNoJson.getString("expressNo"));
                }
                expressList.add(express);
            }
            eceOrder.setExpressNoList(expressList);

        }

        /**************************** Merchandise(商品) ***************************/
        List<EceOrderPackageVo> orderPackageList = new ArrayList<EceOrderPackageVo>();
        if (json.containsKey("orderLineList")) {
            JSONArray olList = json.getJSONArray("orderLineList");
            for (Object object : olList) {
                JSONObject orderLine = (JSONObject) object;
                EceOrderPackageVo packageVo = new EceOrderPackageVo();

                packageVo.setLength(BigDecimal.ZERO);
                packageVo.setWidth(BigDecimal.ZERO);
                packageVo.setHeight(BigDecimal.ZERO);

                if (orderLine.containsKey("grossWeight")) {
                    packageVo.setGrossWeight(orderLine.getBigDecimal("grossWeight"));
                }

                List<EceMerchandiseVo> merchandiseList = new ArrayList<EceMerchandiseVo>();
                if (orderLine.containsKey("merchandiseList")) {
                    JSONArray mlist = orderLine.getJSONArray("merchandiseList");
                    for (Object obj : mlist) {
                        JSONObject merchandise = (JSONObject) obj;
                        EceMerchandiseVo vo = new EceMerchandiseVo();
                        if (merchandise.containsKey("productCode")) {
                            vo.setMerchandiseCode(merchandise.getString("productCode").trim());
                        }
                        if (merchandise.containsKey("specification")) {
                            vo.setMerchandiseSpecification(merchandise.getString("specification").trim());
                        }
                        if (merchandise.containsKey("quantity")) {
                            vo.setMerchandiseQuantity(merchandise.getLong("quantity"));
                        }
                        if (merchandise.containsKey("salePrice")) {
                            vo.setSalePrice(merchandise.getBigDecimal("salePrice"));
                        }
                        if (merchandise.containsKey("currency")) {
                            vo.setCurrencyCode(merchandise.getString("currency").trim());
                        }
                        if (merchandise.containsKey("extraGift")) {
                            vo.setExtraGift(merchandise.getString("extraGift").trim());
                        }
                        if (merchandise.containsKey("brandName")) {
                            vo.setBrandName(merchandise.getString("brandName").trim());
                        }
                        merchandiseList.add(vo);
                    }
                    packageVo.setMerchandiseList(merchandiseList);
                }
                orderPackageList.add(packageVo);
            }
        }

        CommandContext ctx = CommandContext.getContext();
        String source = (String) ctx.get(ApiConstant.API_SOURCE);
        // 设置其他信息，从上下文取得
        eceOrder.setSource(source);
        eceOrder.setBpartnerId(Integer.parseInt(ctx.getUserId()));
        eceOrder.setBpartnerName(ctx.getUsername());
        eceOrder.setOrganizationId(ctx.getOrgId().intValue());

        eceOrder.setOrderPackageList(orderPackageList);
        return eceOrder;
    }

    public static EceOrderInsuranceInfoVo buildEceOrderInsuranceInfoVo(JSONObject json) {
        EceOrderInsuranceInfoVo vo = new EceOrderInsuranceInfoVo();

        if (json.containsKey("orderNo")) {
            vo.setOrderNo(json.getString("orderNo").trim());
        }
        if (json.containsKey("occurTime")) {
            vo.setOccurTime(json.getDate("occurTime"));
        }
        if (json.containsKey("trackingNo")) {
            vo.setTrackingNo(json.getString("trackingNo"));
        }
        if (json.containsKey("messageCode")) {
            vo.setInsureMessageCode(json.getString("messageCode"));
        }
        if (json.containsKey("messageName")) {
            vo.setInsureMessageName(json.getString("messageName"));
        }
        if (json.containsKey("userId")) {
            vo.setCustomerCode(json.getString("userId"));
        }
        if (json.containsKey("extsequenceNo")) {
            vo.setBath(json.getString("extsequenceNo"));
        }
        if (json.containsKey("projectNo")) {
            vo.setProjectNo(json.getString("projectNo"));
        }
        if (json.containsKey("Trade")) {
            vo.setTrade(json.getString("Trade"));
        }
        if (json.containsKey("tranType")) {
            vo.setTranType(json.getString("tranType"));
        }
        if (json.containsKey("riskNo")) {
            vo.setRiskNo(json.getString("riskNo"));
        }

        return vo;
    }
}
